package com.kbbywyclient.ui.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Description: viewpager
 * Created by FHW
 * on 2017/3/29
 */
public class ViewPagerAdapter extends PagerAdapter {

    private List<View> viewContainter; // view页面数
    List<String> titleContainer;
    public ViewPagerAdapter(List<View> viewContainter) {
        this.viewContainter = viewContainter;
    }
    public ViewPagerAdapter(List<View> viewContainter , List<String> titleContainer){
        this.titleContainer=titleContainer;
        this.viewContainter=viewContainter;
    }
    //viewpager中的组件数量
    @Override
    public int getCount() {
        return viewContainter.size();
    }

    //滑动切换的时候销毁当前的组件
    @Override
    public void destroyItem(ViewGroup container, int position,
                            Object object) {
        ((ViewPager) container).removeView(viewContainter.get(position));
    }

    //每次滑动的时候生成的组件
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ((ViewPager) container).addView(viewContainter.get(position));

        return viewContainter.get(position);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public int getItemPosition(Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if(titleContainer==null||titleContainer.size()==0){
            return super.getPageTitle(position);
        }else {
            return titleContainer.get(position);
        }
    }
}
